<template>
  <TableView ref="tableView" :table-attrs="{ api: getList }" :tree-attrs="{ fetchApi: getYearTree, show: true }"
             :delete-api="batchDelete"
             form-path="/asset/fyassetbudget/budgetForm/{id:-1}"
             :toolbar-attrs="{ buttons: ['back',{ del: 'budget_fyassetbudget_del', create: 'budget_fyassetbudget_add' }, 'slot'] }">
    <template #toolbar-button>
      <ToolbarButton privilege="budget_fyassetbudget_export" type="primary" icon="el-icon-download" label="导出" @click="exportExcel()" />
      <ToolbarButton privilege="budget_fyassetbudget_export" type="primary" icon="el-icon-upload" label="导入" @click="$refs.excelUpload.show()" />
    </template>
    <template #columns>
      <TableColumn label="所属年度" prop="year" width="80" />
      <TableColumn label="预算序号" prop="number" clickable width="80" />
      <TableColumn label="预算分类" prop="type" clickable width="80" />
      <TableColumn label="开支单位" prop="expenditureFullname" />
      <TableColumn label="执行单位" prop="executionFullname" />
      <TableColumn label="责任人" prop="liableName" width="115" />
      <TableColumn label="预算金额" prop="amount" width="80" />
      <TableColumn label="完成时间" prop="completionDate" width="95" />
      <TableColumn label="限制合同执行" prop="limitThreshold" width="115" align="center">
        <template #default="{ row }">
          <ElTag>{{ row.limitThreshold === '0' ? '否' : '是' }}</ElTag>
        </template>
      </TableColumn>
      <TableColumn label="备注" prop="remarks" />
    </template>
    <template #search-form>
      <FormInput v-model="searchForm.year" label="所属年度" prop="year" />
      <FormInput v-model="searchForm.number" label="预算序号" prop="number" />
      <FormInput v-model="searchForm.type" label="预算分类" prop="type" />
      <FormInput v-model="searchForm.expenditureFullname" label="开支单位" prop="expenditureFullname" />
      <FormInput v-model="searchForm.executionFullname" label="执行单位" prop="executionFullname" />
      <FormInput v-model="searchForm.liableName" label="责任人" prop="liableName" />
    </template>
    <!--excel 模板导入 -->
    <ExcelUpload
      ref="excelUpload"
      title="资产预算信息导入"
      :url="`${assetService}fyassetbudget/import`"
      :temp-url="`${assetService}fyassetbudget/local/budget.xlsx`"
      temp-name="资产预算信息.xlsx"
      @refreshDataList="refreshChange" />
  </TableView>
</template>

<script>
  import { genPageResponse } from '@/util/response'
  import { services } from '@/config'
  import { fetchList, getYearTree, batchDelete } from '@/api/asset/fyassetbudget'
  import { downBlobFile } from '@/util'

  export default {
    name: 'BudgetView',

    data() {
      return {
        searchForm: {},
        assetService: services.assetService
      }
    },
    methods: {
      batchDelete,
      getYearTree,
      async getList(page) {
        const res = await fetchList(
          Object.assign(
            {
              descs: ['create_time'],
              current: page.current,
              size: page.size
            },
            this.searchForm
          )
        )
        return {
          data: genPageResponse(res.data.data.records)
        }
      },
      refreshChange() {
        this.$refs.tableView.reloadTable()
      },
      exportExcel() {
        downBlobFile(`${services.assetService}fyassetbudget/export`, this.searchForm, `预算管理${this.$dateFormat(new Date())}.xlsx`)
      }
    }
  }
</script>
